﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarbonMeter
{
    public partial class PeopleTransportationConsumptionForm : Form
    {
        public PeopleTransportationConsumptionForm()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            
            if (radioButton1.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(2);
                comboBox30.DataSource = Database.getInstance().getProducts(2);

                radioButton5.Text = "Üretim";
                radioButton6.Text = "Kırsal";
                radioButton7.Text = "Karışık";
                radioButton8.Text = "Kentsel";
                radioButton7.Visible = true;
                radioButton8.Visible = true;
            }
            else if (radioButton2.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(Dahili Proses3);
                comboBox30.DataSource = Database.getInstance().getProducts(3);

                radioButton5.Text = "Üretim";
                radioButton6.Text = "Kırsal";
                radioButton7.Text = "Karışık";
                radioButton8.Text = "Kentsel";
                radioButton7.Visible = true;
                radioButton8.Visible = true;
            }
            else if (radioButton3.Checked)
            {
                //Üretim	Harici Proses	Dahili Proses
                radioButton5.Text = "Üretim";
                radioButton6.Text = "Dahili Proses";
                radioButton7.Text = "Harici Proses";
                radioButton7.Visible = true;
             
                radioButton8.Visible = false;

                //groupBox70.Text = Database.getInstance().getProductGroupName(4);
                comboBox30.DataSource = Database.getInstance().getProducts(4);
            }
            else
            {
                //Üretim	Dahili Proses
                radioButton5.Text = "Üretim";
                radioButton6.Text = "Dahili Proses";
                radioButton7.Visible = false;
                radioButton8.Visible = false;
                //groupBox70.Text = Database.getInstance().getProductGroupName(4);
                comboBox30.DataSource = Database.getInstance().getProducts(5);
            }
        }

        private void PeopleTransportationConsumptionForm_Load(object sender, EventArgs e)
        {
            comboBox30.DisplayMember = "productName";
            comboBox30.ValueMember   = "productId";
            radioButton1_CheckedChanged(  sender,   e);

            this.BringToFront();
            this.WindowState = FormWindowState.Maximized;
        }
        
        private void comboBox30_SelectedIndexChanged(object sender, EventArgs e)
        {
            peopleTransConstant = Database.getInstance().getPeopleTransportationConstant((int)comboBox30.SelectedValue);
            if(tabControl1.SelectedIndex == 0)
                radioButton5_CheckedChanged(sender, e);
            else
                radioButton16_CheckedChanged(sender, e);
        }

        private void button50_Click(object sender, EventArgs e)
        {
            try
            {
                float a = float.Parse(textBox20.Text);

                float b = float.Parse(textBox70.Text);
                textBox100.Text = "" + Math.Round((double)(a * b),2);
                textBox50.Text = "" + Math.Round((double)(a * b * 44 / 12),2);
                button60.Enabled = true;


            }
            catch (Exception)
            {
                MessageBox.Show("Kulllanım Miktarını Düzgün Giriniz.");
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (!((RadioButton)sender).Checked) return;
            }
            catch (Exception)
            {
                
            }
            if (peopleTransConstant == null) return;
            if (radioButton5.Checked)
            {
                textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmManufacture,2);
                
            }
            else if (radioButton6.Checked)
            {
                if (radioButton1.Checked || radioButton2.Checked)
                    textBox70.Text = "" +  Math.Round((double)peopleTransConstant.kgCEqPerKmNonUrban,2);
                else
                {
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCeqPerKmWithProcess,2);
                }
            }
            //TODO: patliyor duzeltilmesi lazim
            else if (radioButton7.Checked)
            {
                if (radioButton1.Checked || radioButton2.Checked)
                    textBox70.Text = ""+Math.Round((double)peopleTransConstant.kgCEqPerKmMixed,2) ;
                else
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCeqPerKmWithoutProcess,2);
            }
            else if (radioButton8.Checked)
            {
                textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmUrban,2);
            }

        }
        PeopleTransportationConstant peopleTransConstant = null;
        private void comboBox30_SelectedValueChanged(object sender, EventArgs e)
        {
           
        }

        private void button60_Click(object sender, EventArgs e)
        {
             Database.getInstance().addPeopleTransportationConsumption(peopleTransConstant.productId,
               float.Parse(textBox20.Text), -1, float.Parse(textBox100.Text), dateTimePicker20.Value);

             dataGridView20.DataSource = Database.getInstance().getPeopleTransportationConsumptionByUserRecordAndTime(MainForm.user.userId, DateTime.Now);
        }

        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton9.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(6);
                comboBox30.DataSource = Database.getInstance().getProducts(6);

                radioButton14.Visible = true;
                radioButton15.Visible = true;
                radioButton16.Visible = true;
                //4 tekerlekli
                radioButton16.Text = "Üretim";
                radioButton15.Text = "Boş Araç";
                radioButton14.Text = "Dolu Araç";
            }
            else if (radioButton10.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(7);
                comboBox30.DataSource = Database.getInstance().getProducts(7);
                radioButton15.Visible = true;
                radioButton16.Visible = true;
                //2 tekerlekli
                radioButton16.Text = "Üretim";
                radioButton15.Text = "Sadece Benzin";
                radioButton14.Visible = false;
            }
            else if (radioButton11.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(9);
                comboBox30.DataSource = Database.getInstance().getProducts(9);
                //Deniz
                radioButton16.Visible = false;
                radioButton15.Visible = false;
                radioButton14.Visible = false;

                textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmMixed,2);
            }
            else if (radioButton12.Checked)
            {
                //groupBox70.Text = Database.getInstance().getProductGroupName(8);
                comboBox30.DataSource = Database.getInstance().getProducts(8);
                //hava
                radioButton16.Visible = false;
                radioButton15.Visible = false;
                radioButton14.Visible = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void radioButton16_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton16.Checked)
            {
                if (radioButton9.Checked || radioButton10.Checked)
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmManufacture,2);
                else if (radioButton12.Checked)
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCeqPerKmWithProcess,2); 
            }
            else if (radioButton15.Checked)
            {
                if (radioButton9.Checked)
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmEmpty,2);
                else if (radioButton10.Checked)
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmMixed,2);
                else if (radioButton12.Checked)
                    textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmNotKyoto,2);
            }
            else if (radioButton14.Checked)
            {
                textBox70.Text = "" + Math.Round((double)peopleTransConstant.kgCEqPerKmFull,2);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                radioButton1_CheckedChanged(sender, e);
                radioButton5_CheckedChanged(sender, e);
            }
            else
            {
                radioButton9_CheckedChanged(  sender,   e);
                radioButton16_CheckedChanged(  sender,   e);
            }
        }
    }
}
